package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import com.itheima.utils.DateUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    MemberService memberService;

    @Reference
    SetmealService setmealService;

    @Reference
    ReportService reportService;

    /**
     * 获取会员统计数据
     * @return
     */
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        try {

            Map<String,Object> map = memberService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /**
     * 获取预约占比
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        /*
           setMealNames:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
           setmealCount:[
                    {value:335, name:'直接访问'},
                    {value:310, name:'邮件营销'},
                    {value:234, name:'联盟广告'},
                    {value:135, name:'视频广告'},
                    {value:1548, name:'搜索引擎'}
                    ]
         */

        try {
            //获取套餐的占比数据
            List<Map<String,Object>> setmealCount = setmealService.getSetmealReport();

            //用于存储套餐名称的集合
            List<Object> setMealNames = new ArrayList<>();

            for (Map<String, Object> map : setmealCount) {
                Object name = map.get("name");
                setMealNames.add(name);
            }

            //构建前端需要的数据返回
            Map<String,Object> setMealDate = new HashMap<>();
            setMealDate.put("setMealNames",setMealNames);
            setMealDate.put("setmealCount",setmealCount);

            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,setMealDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }


    /**
     * 运营数据统计
     */
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    /**
     * 导出运营的数据到本地
     *  1.创建模板
     *  2.获取运营数据
     *  3.把数据写入Excel
     *  4.下载Excel到本地
     *
     *
     */
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response){
        try {
            //获取运营统计数据
            Map<String, Object> businessReportData = reportService.getBusinessReportData();

            Object reportDate = businessReportData.get("reportDate");
            Object totalMember = businessReportData.get("totalMember");
            Object todayNewMember = businessReportData.get("todayNewMember");
            Object thisWeekNewMember = businessReportData.get("thisWeekNewMember");
            Object thisMonthNewMember = businessReportData.get("thisMonthNewMember");
            Object todayOrderNumber = businessReportData.get("todayOrderNumber");
            Object todayVisitsNumber = businessReportData.get("todayVisitsNumber");
            Object thisWeekOrderNumber = businessReportData.get("thisWeekOrderNumber");
            Object thisWeekVisitsNumber = businessReportData.get("thisWeekVisitsNumber");
            Object thisMonthOrderNumber = businessReportData.get("thisMonthOrderNumber");
            Object thisMonthVisitsNumber = businessReportData.get("thisMonthVisitsNumber");
            List<Map<String,Object>> hotSetmeal = (List<Map<String, Object>>) businessReportData.get("hotSetmeal");

            //把数据写入Excel
            //获取到Excel的输入流对象
            InputStream inputStream = this.getClass().getResourceAsStream("/template/report_template.xlsx");

            //获取工作簿对象
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

            //获取工作表
            XSSFSheet sheet = workbook.getSheet("Sheet1");

            //获取行,获取单元格,设置单元格数据
            Row row = sheet.getRow(2);
            Cell cell = row.getCell(5);
            cell.setCellValue(String.valueOf(reportDate));

            //设置今日新增会员数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(4);
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(todayNewMember));

            //设置总会员数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(4);
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(totalMember));

            //设置本周新增会员数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(5);
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(thisWeekNewMember));

            //设置本月新增会员数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(5);
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(thisMonthNewMember));

            //设置今日预约数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(7);
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(todayOrderNumber));

            //设置今日到诊数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(7);
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(todayVisitsNumber));

            //设置本周预约数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(8);
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(thisWeekOrderNumber));

            //设置本周到诊数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(8);
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(thisWeekVisitsNumber));

            //设置本月预约数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(9);
            cell = row.getCell(5);
            cell.setCellValue(String.valueOf(thisMonthOrderNumber));

            //设置本月到诊数
            //获取行,获取单元格,设置单元格数据
            row = sheet.getRow(9);
            cell = row.getCell(7);
            cell.setCellValue(String.valueOf(thisMonthVisitsNumber));

            //声明行号
            int rownum = 12;
            for (Map<String, Object> map : hotSetmeal) {
                //获取行对象
                row = sheet.getRow(rownum);
                //
                cell = row.getCell(4);
                cell.setCellValue(String.valueOf(map.get("name")));
                //获取行，给行赋值
                cell = row.getCell(5);
                cell.setCellValue(String.valueOf(map.get("setmeal_count")));
                //获取行，给行赋值
                cell = row.getCell(6);
                cell.setCellValue(String.valueOf(map.get("proportion")));

                rownum ++ ;

            }

            //4. 下载excel到本地
            // 通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            //指定附件的格式为excel
            response.setContentType("application/vnd.ms-excel");
            //attachment 以附件下载
            //下载弹出框架中的默认的文件名
            response.setHeader("content-Disposition","attachment;filename="+reportDate+"_report.xlsx");
            //输出流下载
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }


    /**
     * 根据输入时间查询会员数量
     */
    @RequestMapping("/getMemberReportByDate")
    public Result getMemberReportByDate(@RequestBody List<Date> date) throws Exception {

        try {
            Map<String,Object> map = memberService.getMemberReportByDate(date);
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /**
     * 根据会员性别展示--饼状图
     */
    @RequestMapping("/getMemberSex")
    public Result getMemberSex(){
        try {
            Map<String,Object> map = memberService.getMemberSex();
            return new Result(true,MessageConstant.GET_S_MEMBERPIC__SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBERPIC_FAIL);
        }

    }

    //会员各个年龄段的占比
    @RequestMapping("/getAgeReport")
    public Result getAgeReport(){
        try {
            List<Map<String,Object>> ageCount = memberService.getAgeReport();

            //用于存储套餐名称的集合
            List<Object> ageNames = new ArrayList<>();

            for (Map<String, Object> map : ageCount) {
                Object name = map.get("name");
                ageNames.add(name);
            }

            //构建前端需要的数据返回
            Map<String,Object> setMealDate = new HashMap<>();
            setMealDate.put("ageNames",ageNames);
            setMealDate.put("ageCount",ageCount);

            return new Result(true,"获取年龄段统计成功",setMealDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取年龄段统计失败");
        }
    }

}
